﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using DAO;
using DTO;

namespace TestGenerating
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.Out.WriteLine("Starting generating data");
            GenDaiLy();
            System.Console.Out.WriteLine("Generating list of DaiLy successfully");
            GenLoaiGas();
            System.Console.Out.WriteLine("Generating list of LoaiGas successfully");
            GenBinhGas();
            System.Console.Out.WriteLine("Generating list of BinhGas successfully");       
            GenPhanPhoi();
          
            UpdateGia();
            GenGiaoHang();
            System.Console.Out.WriteLine("Generating list of GiaoHang successfully");
            System.Console.Out.WriteLine("Generating data successfully");
        }
        private static DataTable dsDaiLy = new DataTable();
       
        private static DataTable dsBinhGas = new DataTable();
        private static DataTable dsVoBinhGas = new DataTable();
        private static void GenDaiLy()
        {
            String[] quan = new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "Tân Bình", "Bình Thạnh", "Tân Phú", "Phú Nhuận", "Gò  Vấp" };
            Random r = new Random(DateTime.Now.Millisecond);
            dsDaiLy.Columns.Add(new DataColumn("ID", typeof(int)));
            dsDaiLy.Columns.Add(new DataColumn("TenDaiLy", typeof(String)));
            for (int i = 0; i < 200; i++)
            {
                DaiLyDTO dto = new DaiLyDTO();
                dto.TenDaiLy = "Đại lý Gas số " + (i + 1);
                dto.Quan = quan[r.Next(quan.Length)];
                dto.DiaChi = "TP. HCM";
                dto.DienThoai = "";                
                DataAccess.InsertDaiLy(dto);
                dsDaiLy.Rows.Add(dto.Id, dto.TenDaiLy);
            }
        }
        private static void GenLoaiGas()
        {            
            Random r = new Random(DateTime.Now.Millisecond);
            Random r1 = new Random(DateTime.Now.Millisecond);
            Random r2 = new Random(DateTime.Now.Millisecond);
                       float[] giaBan = new float[] {150, 160, 170, 180, 190, 200 };
            float[] loiNhuan = new float[] { 15, 10, 20, 25, 30 };
            float[] canNang = new float[] { 50, 55, 60, 65, 70, 75 };
            for (int i = 0; i < 15; i++)
            {
                LoaiGasDTO loaiGas = new LoaiGasDTO();
                loaiGas.TenLoai = "Loại Gas " + (i + 1);
                
                loaiGas.Gia = giaBan[r1.Next(giaBan.Length)];
                loaiGas.CanNang = canNang[r2.Next(canNang.Length)];
                DataAccess.InsertLoaiGas(loaiGas );
                
            }            
        }
        private static void GenBinhGas()
        {
            dsBinhGas.Columns.Add("ID", typeof(int));
            dsBinhGas.Columns.Add("LoaiGas", typeof(int));
            dsBinhGas.Columns.Add("SoLuong", typeof(int));
            Random r = new Random(DateTime.Now.Millisecond);
            String strSql = "select * from LoaiGas";
            DataTable dt = DataAccess.Query(strSql);
            foreach (DataRow row in dt)
            {
                int loaiGas = row["TenLoai"];
                BinhGasDTO bg = new BinhGasDTO()
                {
                    LoaiGas = loaiGas,
                    SoLuong = r.Next(40) + 10
                };
                BinhGasDAO.Them(bg);
                dsBinhGas.Rows.Add(bg.Id, bg.LoaiGas, bg.SoLuong);
            }
        }
        private static void GenVoGas()
        {
            dsVoBinhGas.Columns.Add("ID", typeof(int));
            dsVoBinhGas.Columns.Add("LoaiGas", typeof(int));
            dsVoBinhGas.Columns.Add("SoLuong", typeof(int));
            Random r = new Random(DateTime.Now.Millisecond);
            String strSql = "select * from LoaiGas";
            DataTable dt = DataAccess.Query(strSql);
            foreach (DataRow row in dt)
            {
                int loaiGas = row["TenLoai"];
                VoBinhGasDTO bg = new VoBinhGasDTO()
                {
                    LoaiGas = loaiGas,
                    SoLuong = r.Next(40) + 10
                };
                VoBinhGasDAO.Them(bg);
                dsVoBinhGas.Rows.Add(bg.Id, bg.LoaiGas, bg.SoLuong);
            }
        }
        private static void GenGiaoHang()
        {
            Random r = new Random(DateTime.Now.Millisecond);
            Random r1 = new Random(DateTime.Now.Millisecond);
            float[] taiXe = { "Minh", "Hùng", "Nhân", "Nghĩa", "Trí", "Tín" };
            for (int i = 0; i < 30; i++)
            {
                GiaoHangDTO gh = new GiaoHangDTO()
                {
                    Ngay = DateTime.Today.AddDays(r.Next()),
                    TaiXe = taiXe[r.Next(taiXe.Length)]
                };
                GiaoHangDAO.InsertGiaoHang(gh);
            }
        }
        private static void GenChiTietXeDi()
        {

        }
        private static void GenPhanPhoi()
        {
            int n = 0;
            while(n++<31800)
            {                
                if(n%100==0)
                    System.Console.Out.WriteLine((n + 1)+" record(s) PhanPhoi generated");
                PhanPhoiDTO dto = new PhanPhoiDTO();
                ThuHoiDTO thuHoi = new ThuHoiDTO();
                int nDaiLy = dsDaiLy.Rows.Count;
                int nGas = dsBinhGas.Rows.Count;
                Random r1 = new Random(DateTime.Now.Millisecond);
                Random r2 = new Random(DateTime.Now.Millisecond);
                Random r3 = new Random(DateTime.Now.Millisecond);
                Random r4 = new Random(DateTime.Now.Millisecond);
                int i1, i2, i3, i4;
                i1 = r1.Next(nDaiLy);
                dto.DaiLy = (int)dsDaiLy.Rows[i1]["ID"];
                i2 = r2.Next(nGas);
                dto.LoaiGas = (int)dsBinhGas.Rows[i2]["ID"];
                i3 = r3.Next((int)dsBinhGas.Rows[i2]["SoLuong"]) + 1;
                dto.SoLuong = i3;
                i4 = r4.Next(365*3);                
                int ngay = i4;
                dto.Ngay = DateTime.Now.AddDays(ngay);
                DataAccess.InsertPhanPhoi(dto);
                thuHoi.DaiLy = (int)dsDaiLy.Rows[i1]["ID"];
                thuHoi.Ngay = DateTime.Now.AddDays(ngay);
                thuHoi.VoPhanPhoi = (int)dsBinhGas.Rows[i2]["ID"];
                Random r5 = new Random(DateTime.Now.Millisecond);
                int i5 = r5.Next(3) + 1;
                //i5 1 2 3 4
                float[] phiDoiVo = new float[] { 5, 10, 15};
                int curNum = 0;
                for (int i = 0; i < i5; i++)
                {
                    thuHoi.VoThuVe = (int)dsVoBinhGas.Rows[r2.Next(nGas)]["ID"];
                    thuHoi.SoLuong = r5.Next(i3) + 1;
                    if (curNum + thuHoi.SoLuong > i3)
                        thuHoi.SoLuong = i3 - curNum;
                    thuHoi.PhiDoiVo = 1000*phiDoiVo[r5.Next(phiDoiVo.Length)];
                    DataAccess.InsertThuHoi(thuHoi);
                    curNum += thuHoi.SoLuong;
                    if (curNum >= i3)
                        break;
                }
            }
        }
        private static void UpdateGia()
        {
            float[] giaBan = new float[] { 150, 160, 170, 180, 190, 200 };
            float[] loiNhuan = new float[] { 15, 10, 20, 25, 30 };
            float[] gia = new float[15];
            float[] loi = new float[15];
            Random r1 = new Random(DateTime.Now.Millisecond);
            Random r2 = new Random(DateTime.Now.Millisecond);            
            for (int i = 0; i < 15; i++)
            {
                gia[i] = 1000*giaBan[r1.Next(giaBan.Length)];
                loi[i] = 1000*loiNhuan[r2.Next(loiNhuan.Length)];
                DataAccess.UpdateGiaPhanPhoi(gia[i] + loi[i], gia[i], i + 40);
            }
        }
    }
}

